/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/
#include "header.h"
#include "mypopupmenu.h"
#include "loadsave.h"
#include "mainwindow.h"
#include "menubar.h"

LoadSave::LoadSave(QObject * parent, const char *name):QObject(parent,
							       name),
mainwindow(static_cast < Sopcast * >(parent))
{
    menubar = mainwindow->menubar;
    loadopenstate();
    loadbookmark();
}

LoadSave::~LoadSave()
{
    saveopenstate();
    savebookmark();
}

//-----------------------------------------------------
void
 LoadSave::loadbookmark()
{
    ///assign initial value
    int id = 2;

    QFile fp(QDir::homeDirPath() + "/.qsopcast/bookmark");
    if (fp.open(IO_ReadOnly)) {
	QTextStream stream(&fp);
	stream.setEncoding(QTextStream::UnicodeUTF8);
	while (!stream.atEnd()) {
	    menubar->bookmark->insertItem(stream.readLine(), id);
	    strlst += menubar->bookmark->text(id);
	    id++;
	}
	fp.close();
    }
}

//-----------------------------------------------------
void LoadSave::savebookmark()
{
    ///test change
    bool flag = FALSE;
    if (strlst.count() == menubar->bookmark->count() - 2) {
	QStringList::Iterator it = strlst.begin();
	int index = 2;
	while (it != strlst.end()) {
	    if (menubar->bookmark->
		text(menubar->bookmark->idAt(index)) != *it) {
		flag = TRUE;
		break;
	    }
	    it++;
	    index++;
	}
    } else
	flag = TRUE;
    ///save bookmark
    if (flag == TRUE) {
	QFile fp(QDir::homeDirPath() + "/.qsopcast/bookmark");
	QDir dir(QDir::homeDirPath() + "/.qsopcast");
	if (!dir.exists())
	    dir.mkdir(QDir::homeDirPath() + "/.qsopcast");
	if (fp.open(IO_WriteOnly)) {
	    QTextStream stream(&fp);
	    stream.setEncoding(QTextStream::UnicodeUTF8);

	    for (uint index = 2; index < menubar->bookmark->count();
		 index++)
		stream << menubar->bookmark->text(menubar->bookmark->
						  idAt(index)) << endl;

	    fp.close();
	}
    }
}

//-----------------------------------------------------
void LoadSave::loadopenstate()
{
    QFile fp(QDir::homeDirPath() + "/.qsopcast/tree");
    if (fp.open(IO_ReadOnly)) {
	::read(fp.handle(), mainwindow->stateopen,
	       sizeof(mainwindow->stateopen));
	fp.close();
    } else
	::memset(mainwindow->stateopen, 0, sizeof(mainwindow->stateopen));
}


//-----------------------------------------------------
void LoadSave::saveopenstate()
{
    mainwindow->updateopenstate();

    QString data_path = QDir::homeDirPath() + "/.qsopcast/";
    QDir dir(data_path);
    if (!dir.exists())
	dir.mkdir(data_path);
    data_path += "tree";

    QFile fp(data_path);
    if (fp.open(IO_WriteOnly)) {
	::write(fp.handle(), mainwindow->stateopen,
		sizeof(mainwindow->stateopen));
	fp.close();
    } else
	perror("path to file tree is invalid");
}

//-----------------------------------------------------
